using SharpDX;
using System.IO;
using System.Text;

namespace Engine.FileFormats3D.WR2TerrainLoader
{
    public class WR2Vertex
    {
        #region Private Fields

        private static int staticID;
        private byte[] blendColor;
        private byte[] diffuseColor = new byte[4];
        private int id;
        private byte[] normal = new byte[4];
        private long positionInfile;
        private float positionX;
        private float positionY;
        private float positionZ;
        private float textureU;
        private float textureV;

        #endregion Private Fields

        #region Internal Constructors

        internal WR2Vertex(BinaryReader br)
        {
            positionInfile = br.BaseStream.Position;
            this.id = WR2Vertex.ID;
            positionX = br.ReadSingle();
            positionY = br.ReadSingle();
            positionZ = br.ReadSingle();
            normal = br.ReadBytes(4);
            textureU = br.ReadSingle();
            textureV = br.ReadSingle();
            blendColor = br.ReadBytes(4);
            diffuseColor = br.ReadBytes(4);
            //BlendR,BlendG,BlendB,Shadow,B,G,R,A
        }

        #endregion Internal Constructors

        #region Public Properties

        public static int ID
        {
            get
            {
                WR2Vertex.staticID++;
                return WR2Vertex.staticID;
            }
            set
            {
                ID = value;
            }
        }

        public System.Drawing.Color BlendColor
        {
            get
            {
                return System.Drawing.Color.FromArgb(blendColor[3], blendColor[0], blendColor[1], blendColor[2]);// blendcolor;
            }
        }

        public Vector3 Normal
        {
            get { return new Vector3(normal[0] / 128.0f, normal[1] / 128.0f, normal[2] / 128.0f); }
            set { normal[0] = (byte)(value.X * 128); normal[1] = (byte)(value.X * 128); normal[2] = (byte)(value.X * 128); }
        }

        public Vector3 Position
        {
            get { return new Vector3(this.positionX, this.positionY, this.positionZ); }
            set { this.positionX = value.X; this.positionY = value.Y; this.positionZ = value.Z; }
        }

        public Vector2 UvCoordinates
        {
            get { return new Vector2(this.textureU, this.textureV); }
            set { this.textureU = value.X; this.textureV = value.Y; }
        }

        #endregion Public Properties

        #region Public Methods

        public static string ByteToString(byte[] bytes)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < bytes.Length; i++)
            {
                sb.Append(bytes[i].ToString() + " ");
            }
            return sb.ToString();
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("Vertex:        " + this.id.ToString() + " at Position: " + this.positionInfile.ToString());
            sb.AppendLine("PositionX:     " + this.positionX.ToString());
            sb.AppendLine("PositionY:     " + this.positionY.ToString());
            sb.AppendLine("PositionZ:     " + this.positionZ.ToString());
            sb.AppendLine("Normal:        " + ByteToString(this.normal));
            sb.AppendLine("U:             " + this.textureU.ToString());
            sb.AppendLine("V:             " + this.textureV.ToString());
            sb.AppendLine("Blend Color:   " + ByteToString(this.blendColor));//.ToString());//.ToString());
            sb.AppendLine("Diffuse Color: " + this.diffuseColor.ToString());

            return sb.ToString();
        }

        #endregion Public Methods
    }
}